using System.Collections.Generic;
using UnityEngine;

namespace Gameplay.PVE
{
    public class VectorMath
    {
        public static bool IsColinearXZ (Vector3 a, Vector3 b, Vector3 c) {
            float v = (b.x-a.x)*(c.z-a.z)-(c.x-a.x)*(b.z-a.z);

            // Epsilon not chosen with much thought, just that float.Epsilon was a bit too small.
            return v <= 0.0000001f && v >= -0.0000001f;
        }
        public static bool RightOrColinearXZ (Vector3 a, Vector3 b, Vector3 p) {
            return (long)(b.x - a.x) * (long)(p.z - a.z) - (long)(p.x - a.x) * (long)(b.z - a.z) <= 0;
        }
    }

    public static class Polygon
    {
        public static void Subdivide(List<Vector3> points, List<Vector3> result, int subSegments)
        {
            for (int i = 0; i < points.Count - 1; i++)
            for (int j = 0; j < subSegments; j++)
                result.Add(Vector3.Lerp(points[i], points[i + 1], j / (float) subSegments));

            result.Add(points[points.Count - 1]);
        }
    }

    public static class AstarSplines
    {
        public static Vector3 CubicBezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
        {
            t = Mathf.Clamp01(t);
            float t2 = 1 - t;
            return t2 * t2 * t2 * p0 + 3 * t2 * t2 * t * p1 + 3 * t2 * t * t * p2 + t * t * t * p3;
        }
    }
}